﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Core.Support;
using MS.InformationManagement.DAL.RecordCenter;
using Microsoft.SharePoint;

namespace MS.InformationManagement.BLL.TRD
{
    public class UpdateTRDItemCommand : IBaseCommand
    {
        #region [Private Fields]

        private Stack<ICommand> _Commands;

        private int? _Id;

        private string _Title;
        private string _Law;
        private string _ChangeNotes;
        private string _Changes;

        private DataContext _DataContext;

        private FondosFondo _Backdrop;
        private FondosFondo _ChildBackdrop;
        private SeccionesSeccion _Section;
        private SeccionesSeccion _Subsection;
        private Serie _Serie;
        private Subserie _Subserie;

        private double _GeneralStorageTime;
        private double _CentralStorageTime;

        private bool _ShouldBeKept;
        private bool _ShouldBeSelected;
        private bool _ShouldBeDeleted;
        private bool _ShouldBeTechnicalMean;

        private List<TipoDocumental> _DocumentTypes;

        #endregion

        #region [Constructor]

        public UpdateTRDItemCommand(int? id, string title, FondosFondo backdrop, FondosFondo childBackdrop, SeccionesSeccion section
            , SeccionesSeccion subsection, Serie serie, Subserie subserie, double generalStorageTime, double centralStorageTime, bool shouldbeKept, bool shouldbeSelected
            , bool shouldbeDeleted, bool shouldbeTechnicalMean, string law, List<TipoDocumental> documentTypes, string changeNotes, DataContext dataContext)
        {

            _Commands = new Stack<ICommand>();

            _Id = id;
            _Backdrop = backdrop;
            _CentralStorageTime = centralStorageTime;
            _ChangeNotes = changeNotes;
            _ChildBackdrop = childBackdrop;
            _DocumentTypes = documentTypes;
            _GeneralStorageTime = generalStorageTime;
            _Law = law;
            _Section = section;
            _Serie = serie;
            _ShouldBeDeleted = shouldbeDeleted;
            _ShouldBeKept = shouldbeKept;
            _ShouldBeSelected = shouldbeSelected;
            _ShouldBeTechnicalMean = shouldbeTechnicalMean;
            _Subsection = subsection;
            _Subserie = subserie;
            _Title = title;

            _Commands = new Stack<ICommand>();

            _DataContext = dataContext;
        }

        #endregion

        #region [IBaseCommand Members]

        public void Execute()
        {

            try
            {
                DisableTRDItemCommand dCommand = new DisableTRDItemCommand(_Id, _DataContext);
                CreateTRDItemCommand cCommand = new CreateTRDItemCommand(_Id, _Title, _Backdrop, _ChildBackdrop, _Section, _Subsection, _Serie, _Subserie, _GeneralStorageTime
                    , _CentralStorageTime, _ShouldBeKept, _ShouldBeSelected, _ShouldBeDeleted, _ShouldBeTechnicalMean, _Law, _DocumentTypes, _ChangeNotes, _DataContext);

                dCommand.Execute();
                _Commands.Push(dCommand);

                cCommand.Execute();
                _Commands.Push(cCommand);
            }
            catch (Exception ex)
            {
                this.Undo();
                throw;
            }

        }

        public void Undo()
        {
            while (_Commands.Count != 0)
            {
                _Commands.Pop().Undo();
            }
        }

        #endregion
    }
}
